package com.ngkj.zmfptback.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptback.sys.bean.OperationEntity;
import com.ngkj.zmfptback.sys.service.read.OperationReadService;
import com.ngkj.zmfptback.sys.service.write.OperationWriteService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author ChenHN
 * @since 2021/12/23 creation
 **/
@RestController
@RequestMapping("/oper")
public class OperController {

    @Resource
    private OperationReadService operationReadService;

    @Resource
    private OperationWriteService operationWriteService;

    @GetMapping("/getOperList")
    public CommonReslut getOperList(OperationEntity operationEntity,int pageSize,int pageNo){
        try {
            Page<OperationEntity> page = new Page<>(pageNo,pageSize);
            IPage<OperationEntity> iPage = operationReadService.getOperList(page,operationEntity);
            return new CommonReslut(ResultEnum.ok,iPage);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }


    @PostMapping("/editOper")
    public CommonReslut editOper(@RequestBody OperationEntity operationEntity){
        try {
            operationWriteService.editOper(operationEntity);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/addOper")
    public CommonReslut addOper(@RequestBody OperationEntity operationEntity){
        try {
            operationWriteService.addOper(operationEntity);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @DeleteMapping("deleteOper")
    public CommonReslut deleteOper(Long id){
        try {
            operationWriteService.deleteOper(id);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut<>(ResultEnum.error);
        }
    }

}
